package cn.tjnext.apply.web.controller;

import cn.tjnext.apply.contants.*;
import cn.tjnext.apply.entity.ApplicantEntity;
import cn.tjnext.apply.entity.PayCallBackEntity;
import cn.tjnext.apply.entity.UserEntity;
import cn.tjnext.apply.service.ApplicantService;
import cn.tjnext.apply.service.PayCallBackService;
import cn.tjnext.apply.utils.Converter;
import cn.tjnext.apply.utils.RSASig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import static org.springframework.data.jpa.domain.AbstractPersistable_.id;

/**
 * 支付
 * Created by cp on 2017/8/14.
 */
@Controller
@RequestMapping(value = "/payment")
public class PaymentController {

    @Value("PUBKEY")
    private static String STRPUBKEY;

    @Autowired
    private ApplicantService applicantService;

    @Autowired
    private PayCallBackService payCallBackService;
    /**
     * 支付通知
     * @param request
     * @param response
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/callback", method = RequestMethod.POST)
    public void getCallBack(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("***********************商户通知***************************");
        System.out.println("query: " + request.getQueryString());
        System.out.println("ORDERID: " + request.getParameter("ORDERID"));
        System.out.println("SUCCESS: " + request.getParameter("SUCCESS"));
        System.out.println("ACCDATE: " +request.getParameter("ACCDATE"));
        System.out.println("SIGN: " +request.getParameter("SIGN"));
        Long orderId = Long.valueOf(request.getParameter("ORDERID"));
        String payStatus = request.getParameter("SUCCESS");
//         验签 todo
//         报名信息
        List<ApplicantEntity> applicantEntities = applicantService.findByOrderId(orderId);
        if (applicantEntities.size() > 0 && PayStatus.SUCCESS.equals(payStatus)) {
            for (ApplicantEntity applicantEntity : applicantEntities) {
                applicantEntity.setStatus(EntityStatus.NORMAL);
                applicantService.createOrSave(applicantEntity);
                PayCallBackEntity payCallBackEntity = new PayCallBackEntity();
                payCallBackEntity.setOrderId(orderId);
                payCallBackEntity.setCreateTime(Converter.urlToDate(request.getParameter("ACCDATE")));
                payCallBackEntity.setSign(request.getParameter("SIGN"));
                payCallBackEntity.setPayStatus(EntityStatus.NORMAL);
                payCallBackEntity.setStatus(EntityStatus.NORMAL);
                payCallBackService.createOrSave(payCallBackEntity);
            }
        }else {
            System.out.println("报名信息不存在");
        }
        System.out.println("***********************商户通知***************************");
    }

    /**
     * 建行支付完成页面返回报名页
     * @param model
     * @return
     */
    @RequestMapping(value = RequestMappingPaths.TOSIGN)
    public String toSign(HttpServletRequest request, Model model) {
        model.addAttribute(BaseModelAttributeNames.DATA_LIST, ProductConstants.getProductList);
        return PagePaths.APP + PagePaths.SIGN;
    }

    /**
     * 验签
     * @param strSrc
     * @param strPubKey
     * @param strPriKey
     * @return
     */
//    private boolean sign(String strSrc, String strPubKey, String strPriKey) {
//        RSASig rsa=new RSASig();
//        rsa.setPrivateKey(strPriKey);
//        String strSign = rsa.generateSigature(strSrc);
//        System.out.println(strSign);
//
//        rsa.setPublicKey(strPubKey);
//        if( rsa.verifySigature( strSign,strSrc) ){
//            return true;
//        }
//        else{
//            return false;
//        }
//    }
}
